<?php 
session_start();
include 'phpfunctions.php';
include('data/lib_f_pictures.php');
include('data/lib_f_validate.php');
if ($_SESSION['admin'])
{
	$edit = '1';
}

function checkem(&$item, &$description,&$short_description,&$cost_track,&$cost_day)
{
	if($cost_track && !$cost_track = validateText($cost_track)){$err .= 'Track cost is invalid!<br>';}
	if($cost_day && !$cost_day = validateText($cost_day)){$err .= 'Cost per day is invalid!<br>';}
	if($short_description && !$short_description = validateText($short_description)){$err .= 'Short description day is invalid!<br>';}
	if($item && !$item = validateText($item)){$err .= 'Item name is invalid!<br>';}
	return $err;
}

if($_POST['submit'])
{
	extract($_POST);
	echo 'Updating details of ' . $item . ' with track price:' . $cost_track . ' and day price:' . $cost_day .'with mini description: ' . $short_description;
	$err = checkem($item, $description,$short_description,$cost_track,$cost_day);
	if(!$err)
	{
		$db = getdb();
		$query = 'UPDATE items SET trip_price="'.$cost_track.'", day_price='.($cost_day ? '"'.$cost_day.'"' : 'NULL' ).', short_description="'.$short_description.'", item_name="'.$item.'" WHERE id="'.$id.'"';
		dm($query);
		$result = mysql_query($query);
	} else {
		echo $err;
	} 
}

if($POST['create'])
{
	extract($_POST);
	echo 'Creating item: ' . $item . ' with track price:' . $cost_track . ' and day price:' . $cost_day .' with mini description: ' . $short_description;
	$err = checkem($item, $description,$short_description,$cost_track,$cost_day);
	if(!$err)
	{
		$db = getdb();
		echo ' inserting into DB<br>';
		$query = '';
	}
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Bev's Tramping Gear Hire - Price List</title>
<link href="template.css" rel="stylesheet" type="text/css" />
</head>
<?php
$db = getdb();
$table = "items";
$query = "SELECT * FROM " . $table;
$result = mysql_query($query);
$num_items = mysql_numrows($result);
dm($query . " : "  . $result . "<br>");
$item = mysql_result($result,0,"item_name");
$id = mysql_result($result,0,"id");
$cost_day = mysql_result($result,0,'day_price');
$cost_track= mysql_result($result,0,'trip_price');
$short_description = mysql_result($result,0,'short_description');
mysql_close();
//$brands = getBrandsFromIDs(mysql_result($result,0,'brand_ids'));
?>
<body>
<div id="container">
	<div id="header">	</div>
		<div id="nav">
    <?php include 'navbar.php';?>
    </div>
<div id="content">
  <div class="style1" id="item">
  <?php echo ($edit)?'<div align="center">Leave edit mode? <form action="login.html" method="POST" enctype="multipart/form-data"><input type="submit" name="logout" id="logout" value="Logout" /></form></div>':' '; ?>
  <div align="center">
    <table width="100%" border="1" padding="2" id="table_prices">
      <tr class="heading" id="pricelist">
        <td width="29%">Item Name</td>
        <td width="29%">Track Charge (3 - 4 Days)</td>
        <td width="29%">Day Charge (Overnight counts as two days)</td>
        <?php echo ($edit)?'<td width="10%">Change</td>':'';?> 
      </tr>
      <?php
	$i=0;
	while ($i < $num_items)
	{
		$id = mysql_result($result,$i,"id");
		$cost_day = mysql_result($result,$i,'day_price');
		$cost_track= mysql_result($result,$i,'trip_price');
		$short_description = mysql_result($result,$i,'short_description');
		$item = mysql_result($result,$i,"item_name");
		echo '
	<tr id="'.$id.'"><td>';
		echo ($edit)?'<form id="'.$id.'" action="'.$_SERVER['PHP_SELF'].'" method="POST">':'';
		echo ($edit)?'<input type=hidden name="id" value="'.$id.'">':'';
		$link = '<a href="./item.php?id=' . $id . '">' . $item.'</a>';
		echo ($edit)?'Go edit Item: ': '';
		echo $link;
		echo ($edit)?'<br><input name="item" type="text"  id="item"  value="'.$item.'" size="30" maxlength="255" style="width:400px;"/>':'';
		echo ($edit)?'<br><input name="short_description" type="text"  id="short_description"  value="'.$short_description.'" size="30" maxlength="255" style="width:400px;"/>':'<br><em class=style1 >'.$short_description.'</em>';
		
		echo '</td><td>';
		echo ($edit)?'<input name="cost_track" type="text"  id="cost_track" value="'.$cost_track.'" size="5" /></td>':'';
		echo ($cost_track)? "$".$cost_track . '.00</td>':'N/A';
        echo '<td>';
		echo ($edit)?'<input name="cost_day" type="text"  id="cost_day" value="'.$cost_day.'" size="5" /></td>': '';
		echo ($cost_day)? '$'.$cost_day . '.00</td>':'N/A';
		echo ($edit)?'<td><input type="submit" name="submit" id="submit" value="change" /></td></tr></form>':'</tr>';
		$i++;
     }
	if($edit)
	{
		$id=$num_items;
		echo'<tr id="newitem">
				<form id="'.$id.'" action="'.$_SERVER['PHP_SELF'].'" method="POST"><input type=hidden name="id" value="'.$id.'">
				<td width="29%">New Item Name:<input name="item" type="text"  id="item"  value="item name" size="30" maxlength="255" style="width:300px;"/><br>Mini Description: <input name="short_description" type="text" id="short_description" value="" size="30" maxlength="255" style="width:300px;" /></td>
        		<td width="29%">Track Charge:<input name="cost_track" type="text"  id="cost_track" value="" size="5" /></td>
        		<td width="29%">Day Charge: <input name="cost_day" type="text"  id="cost_day" value="" size="5" /></td>
				<td><input type="submit" name="create" id="create" value="Create New Item" /></td>
				</form>
			 </tr>';
	} 
?>
      
    </table>
  </div>  
  </div>
  <div align="center" class="style1" id="footer">
      <p><strong><a href="http://www.fiordland.org.nz/">Visit Destination Fiordland for all your information on Fiordland and Te Anau<br/>
      </a></strong></p>
      2007 webdesign by <a href="http://justcreations.co.nz">Justcreations</a> &amp; Brian Thorne, All rights reserved, Any technical problems email the <?php echo email_link('Brian', 'webmaster', 'Email Brian the webmaster','Enquiry from bevs-hire website'); ?>  </div>
</div>
</div>
</body>
</html>
